<%@page import="edcom.server.*"%>
<%@page import="edcom.client.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>requestAddStudent</title>
</head>
 <body>

<h1>TESTBENCH</h1>
<h3>deleteImage</h3>
<a href="index.jsp">RETURN</a>
<br /><br />

<!-- (DevTeam, be sure to clear the tables before accessing this page.) -->

function declaration:<br/>
<br/>
<code>String deleteImage(TaggedImage img)</code><br/>
<br/>
<br/>
returns: &nbsp;
	<ul>
		<li>String "success" if the parameter <code>img</code> exists in the server folder and is registered in the database</li>
		<li>An error message string starting with ("ERROR: ...") if the process encountered an error</li>
	</ul>
<br/>
<br/>
NOTE: <br/>
The algorithm only checks the <code>id</code>, <code>extension</code> and <code>className</code> (<code>course</code> in the database) <br/>
fields of the TaggedImage.
<br/>
<br/>
WARNING: if RUN#1 is successfully executed, the server HAD ALREADY DELETED the actual file <br/> and unregistered it from the database.<br/>
Be sure to reset the <code>images</code> database tables and paste the proper images in the <br/>correct folder before loading this page.
<br/>
<br/>

<br/><br/>
<b>Run #1 :: returns "success"</b><br/>
reason: existing <code>img</code> parameter <br/>(<code>TaggedImage img = new TaggedImage("65fcf2e4e93be59d26b748a20d72c7b2", ".jpg", "demo", "", "");</code>)
<%
ImageServiceImpl impl = new ImageServiceImpl();

TaggedImage img = new TaggedImage("65fcf2e4e93be59d26b748a20d72c7b2", ".jpg", "demo", "", ""); 
String result = "";
try {
// 	result = impl.deleteImage(img, request, response); /*
	result = impl.deleteImage(img, request.getRealPath("/")); // */
} catch (Exception e) {
	result = e.getMessage();
}

out.println("<table border='1'>");
out.print("<tr><td>result</td><td>" + result + "</td></tr>");
out.println("</table>");
%>

<br/><br/>
<b>Run #2 :: returns error message</b><br/>
reason: nonexistent <code>img</code> parameter <br/>(<code>TaggedImage img = new TaggedImage("nonexiststent", "", "demo", "", "");</code>)
<%
impl = new ImageServiceImpl();

img = new TaggedImage("nonexiststent", "", "demo", "", ""); 
try {
	result = impl.deleteImage(img, request.getRealPath("/"));
} catch (Exception e) {
	result = e.getMessage();
}

out.println("<table border='1'>");
out.print("<tr><td>result</td><td>" + result + "</td></tr>");
out.println("</table>");
%>

<br/><br/>
<b>Run #3 :: does not return "success"</b><br/>
reason: <code>img</code> parameter is null
<%
impl = new ImageServiceImpl();

try {
	result = impl.deleteImage(null, request.getRealPath("/"));
} catch (Exception e) {
	result = e.getMessage();
}

out.println("<table border='1'>");
out.print("<tr><td>result</td><td>" + result + "</td></tr>");
out.println("</table>");
%>

<br/>
<br/>

<br/>
NOTE: <br/>
Unlike the testcases above, the actual GWT application calls this in this way:<br/>
<br/>
<code>deleteImage(img, null)</code><br/>
<br/>
The first parameter <code>img</code> is the image reference information and second handles a string <br/>
that is used to correctly locate the "real" path of the image to be deleted (which is something like<br/>
&lt;your_tomcat_directory&gt;webapps/ROOT) and this is done by JSP using <code>request.getRealPath("/")</code>.<br/>
<br/>
The client-side GWT application CANNOT call the function in this manner since the <code>request</code> <br/>
is a JSP Servlet variable and GWT has no way of accessing it in the same JSP page where it is used.<br/>
<br/>
Luckily, GWT Remote Procedure Calls (RPC) implementations make use of Servlets. When the RPC is invoked, <br/>
the GWT app tells the server to invoke the class as a Servlet (notice the <code>extends RemoteServiceServlet</code> <br/>
at line 10 of ROOT/src/edcom/server/ImageServiceImpl.java) and that instance will find the real path <br/>
of the current page which is &lt;your_tomcat_directory&gt;webapps/ROOT (notice the if-null check at <br/>
line 77 of ImageServiceImpl.java) using its own <code>request</code> variable.<br/> 
<br/>
This way, both GWT apps and JSP can use the functionality, but the GWT application has to use <code>null</code><br/>
as the second parameter. (To see this, look up line 253 of ROOT/src/edcom/client/ImageViewer.java<br/>
(the third parameter (please don't mind it) is just an RPC handle so we can make use of the function's return values).)<br/>
<br/>
For the sake of completion, we have inculded below an example function call that could be used by a GWT RPC.<br/>
<br/>

<br/><br/>
<b>Run #4 :: throws an Exception (might print <code>null</code>)</b><br/>
reason: existing <code>img</code> parameter <br/>
(<code>TaggedImage img = new TaggedImage("65fcf2e4e93be59d26b748a20d72c7b2", ".jpg", "demo", "", "");</code>)<br/>
but the second paramter is <code>null</code>, therefore telling the process that it has to find the <br/>
correct file path as a Servlet.  
<%
impl = new ImageServiceImpl();

img = new TaggedImage("65fcf2e4e93be59d26b748a20d72c7b2", ".jpg", "demo", "", ""); 
result = "";
try {
	result = impl.deleteImage(img, null);
} catch (Exception e) {
	result = e.getMessage();
}

out.println("<table border='1'>");
out.print("<tr><td>result</td><td>" + result + "</td></tr>");
out.println("</table>");
%>

<br/>
<br/>
<br/>
<br/>

</body>
</html>